<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$return['pluginset']['merchfooternav']=$this->model('diy')->merchfooternav($_SESSION['merchid'],$return['pluginset']);
	$couponset=plugin('jy_weishop')->plugin_setting('coupon');
	$coupontype=intval($_RQ['coupontype']);
	$isallcan=intval($_RQ['isallcan']);
	$merchid=intval($_RQ['merchid'])>0?intval($_RQ['merchid']):0;
	$pindex = max(1, intval($_RQ['page']));
	$psize = 8;
	$total='';
	if($coupontype !=10 || $merchid || $isallcan){
		$params = array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'status'=>1);
		$levelinfo=pdo_getcolumn('jy_weishop_member',array('merchid'=>$merchid,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'level');//会员等级信息
		$params['memberlevels OR']='islimitlevel=0 or memberlevels=\'\' or memberlevels like \'%;'.trim($levelinfo).';%\'';//等级领取权限
		if($coupontype){
			$params['coupontype']=$coupontype;
		}
		if($merchid || $isallcan){
			$params['merchid OR']='merchid = \''.$merchid.'\' or ismerch >\'0\'';
			$return['params']['merchid']=$merchid;
		}
		if($merchid || $isallcan){
			$getcoupon=pdo_getall('jy_weishop_coupon',$params,array(),'','id desc');
		}else{
			$getcoupon=pdo_getslice('jy_weishop_coupon',$params,array($pindex,$psize),$total,array(),'','id desc');
		}
		foreach($getcoupon as $k=>&$item){
			$limittimes2=json_decode($item['limittimes'],true);
			$item=model('jy_weishop/coupon')->intcoupon($item['id'],'',$merchid);
			if($item['issendtime']){
				$item['sendtime']=$item['sendtime']?json_decode($item['sendtime'],true):array();
				$item['etime']=$item['sendtime']['tend'];
				if($item['sendtime']['tstart']>time()){
					$item['issend']=0;
					$item['etime']=$item['sendtime']['tstart'];
				}elseif($item['sendtime']['tstart']<time() && $item['sendtime']['tend']>time()){
					$item['issend']=1;
					$item['etime']=$item['sendtime']['tend'];
				}elseif($item['sendtime']['tend']<time()){
					$item['issend']=2;
				}
				$item['sendtime']['tend2']=$item['sendtime']['tend'];
				$item['sendtime']['tstart2']=$item['sendtime']['tstart'];
				$item['sendtime']['tend']=date('Y-m-d',$item['sendtime']['tend']);
				$item['sendtime']['tstart']=date('Y-m-d',$item['sendtime']['tstart']);
			}else{
				$item['issend']=1;
			}
			if($item['getmax'] > 0){
				$num = pdo_count('jy_weishop_coupon_getlog',array('uniacid' => $_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'couponid'=>$item['id']));
				if($num<$item['getmax']){
					$item['canget'] = 1;
				}else{
					$item['canget'] = 0;
				}
			}else{
				$item['canget'] = 1;
				$item['getmax'] = '不限';
			}
			if($item['nomerch']){
				unset($getcoupon[$k]);
				continue;
			}
			if($isallcan && ($item['isdisa']==1 || $item['issend']!=1 || !$item['canget'])){
				unset($getcoupon[$k]);
				continue;
			}
			if(($item['timelimittype']==2 && $limittimes2['tend']<time())){
				unset($getcoupon[$k]);
			}else{
				$item['settitlecolor']=$item['settagtitlecolor'];
				$couponids[]=$item['id'];
				$coupon[]=$item;
			}
		}
		
	}else{
		$params = array('uniacid'=>$_SESSION['uniacid'],'gettype'=>1);
		$coupon=pdo_getslice('jy_weishop_wxcard',$params,array($pindex,$psize),$total,array(),'','id desc');
		foreach($coupon as &$row){
			if($row['card_type'] =='CASH'){
				$row['color'] = 'pink ';
				$tagtitle = '代金券';
				$title2 = '满'.$row['least_cost'].'可用';
				$title3 = '立减';
				$title5 = '消费满￥' . (double) $row['least_cost']. '立减' . (double) $row['reduce_cost'].'元';
			}else{
				$row['color'] = 'pink ';
				$tagtitle = '折扣券';
				$title2 = '满'.$row['least_cost'].'可用';
				$title3 = '立减';
				$title5 = '消费满￥' . (double) $row['least_cost']. '打' . (double) $row['discount'].'折';
			}
			if ($row['datetype'] == '0') {
					$title4 = '即' . $row['fixed_term'] . '天有效';
			}else{
					$row['timestamp']=json_decode($row['timestamp'],true);
					$title4 = $row['timestamp']['tstart'].'~'.$row['timestamp']['tend'];
			}			
			$row['title2'] = $title2;
			$row['title3'] = $title3;
			$row['title4'] = $title4;
			$row['title5'] = $title5;
			$row['settitlecolor']=$row['settagtitlecolor'];
		}
		//用户领取卡券
		$res=cfc('account')->request('https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={access_token}&type=wx_card');
		$api_ticket = $res['ticket'];
		$params=array(
			'api_ticket'=>$api_ticket,
			'timestamp'=>time(),
			'card_id'=>$row['card_id'],
		);
		$signature = sort($params);
		$cardExt = array('openid'=>$_SESSION['openid'],'timestamp'=>time(),'outer_str'=>'jy_weishop','signature'=>$signature);
		$row['cardExt'] = json_decode($cardExt,true);
	}
	$return['coupontype']=$coupontype;
	$return['couponset']=$couponset;
	$return['couponids']=$couponids;
	$return['list']=$coupon?:array();
	$return['ntime']=time();
	$return['ntime']=time();
	$title=$return['setting']['title']?'优惠券-'.$return['setting']['title']:'优惠券-领取中心';
	$sharelink='jy_merch/coupon.index//fromuser='.$_SESSION['uid'];
	$return['sharedata']=array(
			'title'=>$title,
			'desc'=>$return['setting']['share_desc']?:$return['setting']['title'].'在线商城，一个属于自己的个人商城！',
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>tomedia($return['setting']['share_image'])
	);
	set_html('title',$title);
	exi($return);
}
if($_RQ['op']=='tx'){
	$post=get_postdata();
	$id = intval($post['id']);
	pdo_insert('jy_weishop_sendmsg',array('mid'=>$_SESSION['uid'],'couponid' => $id,'time'=>time(),'issend'=>0,'msg'=>'优惠券领券提醒！'));
	exi('设置成功');
}
//整理优惠券数据，先获取用户所有待使用优惠券，再从中筛选出未过期的优惠券且本次下单可用的优惠券！
if($_RQ['op']=='getmycoupon_ing'){
	$order=get_postdata();
	$return=array();
	if(!$order['no_coupon_discount']){
		//整理优惠券数据，先获取用户所有待使用优惠券，再从中筛选出未过期的优惠券且本次下单可用的优惠券！
		$newcoupons=model('jy_weishop/coupon')->getmycoupon_ing(1,$order['total_price'],$order['level_de_price'],$order['list'],$order['enough_de_price']);
		$return['coupon_de_price']=$newcoupons[0]['de_price'];
		$return['couponid']=$newcoupons[0]['id'];
		$return['couponcount']=count($newcoupons);
	}
	$return['coupons']=$newcoupons?$newcoupons:array();
	exi($return);
}
//领券优惠券
if($_RQ['op']=='getall'){
	$post=get_postdata();
	$couponids=$post['couponids'];
	foreach($couponids as $id){
		model('jy_weishop/coupon')->getcoupon($id);
	}
	if($post['nogoback']){
		exi('成功领取优惠券.');
	}else{
		$merchid=$_SESSION['merchid']?:0;
		exi('成功领取优惠券！','','goback');
	}
}
?>